package com.yuanchu.marketing.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.marketing.dto.CouponParamsDto;
import com.yuanchu.marketing.entity.YoshopUserCouponEntity;
import com.yuanchu.marketing.service.YoshopUserCouponService;
import com.yuanchu.marketing.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-09 10:29
 */
@Api(tags = "user优惠券接口 管理")
@RestController
@RequestMapping("coupon")
public class UserCouponController {

    @Resource
    private YoshopUserCouponService userCouponService;

    int storeId = 10001;
    @ApiOperation(value = "/user/{couponId}", tags = "获取优惠券详情")
    @GetMapping("user")
    public List<YoshopUserCouponEntity> queryUserCoupon(@RequestParam(value = "couponId", required = false) Integer couponId, @RequestParam("userId") Integer userId){
        List<YoshopUserCouponEntity> coupon = userCouponService.queryUserCouponById(couponId, userId);
        return coupon;
    }
    @ApiOperation(value = "/user/list", tags = "获取用户优惠券列表")
    @GetMapping("/user/list")
    public R queryUserCouponList(@RequestParam("dataType") String dataType, @RequestParam("page") Integer page){
        Integer userId = SecurityUtil.getUserId();
        PageResult<YoshopUserCouponEntity> userCoupons = userCouponService.queryUserCouponList(dataType, page, userId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", userCoupons);
        return R.ok().put("data", map);
    }

    // marketing/coupon/user/receive
    @ApiOperation(value = "coupon/user/receive", tags = "领取优惠券")
    @PostMapping("/user/receive")
    public R receiveCoupon(@RequestBody @Validated({ValidationGroups.Update.class}) CouponParamsDto params){
        Integer userId = SecurityUtil.getUserId();
        return userCouponService.receiveCoupon(params.getCouponId(), userId, storeId);
    }


    @ApiOperation(value = "coupon/user/status", tags = "修改优惠券状态")
    @PostMapping("/user/status")
    public R updateUserCouponStatus(Integer userId, Integer couponId, Integer storeId){
        boolean update = userCouponService.updateUserCouponStatus(userId, couponId, storeId);
        if (!update){
            return R.error("优惠券状态修改失败");
        }
        return R.ok();
    }

    // @ApiOperation(value = "coupon/user/edit", tags = "修改优惠券信息")
    // @PostMapping("/user/edit/{couponId}")
    // public R editCoupon(@PathVariable("couponId") Integer couponId){
    //     Integer userId = SecurityUtil.getUserId();
    //     boolean edit = userCouponService.editCoupon(couponId, userId, storeId);
    //     if (!edit){
    //         return R.error("修改优惠券失败");
    //     }
    //     return R.ok("修改优惠券成功");
    // }
}
